package com.cruise.game.controller.admin;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.common.constants.CommonConstants;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.param.admin.UserStatusParam;
import com.cruise.game.model.vo.admin.UserDetailVO;
import com.cruise.game.model.vo.admin.UserListVO;
import com.cruise.game.model.vo.common.PageResult;
import com.cruise.game.service.admin.AdminUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 管理员用户控制器
 * 
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "管理员用户相关接口")
@RestController
@RequestMapping("/api/admin/users")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class AdminUserController extends BaseController {

    @Resource
    private AdminUserService adminUserService;

    /**
     * 获取用户列表
     */
    @ApiOperation("获取用户列表")
    @GetMapping
    public ApiResult<PageResult<UserListVO>> getUserList(
            @ApiParam(value = "用户角色", required = false) @RequestParam(value = "role", required = false) String role,

            @ApiParam(value = "账号状态", required = false) @RequestParam(value = "status", required = false) String status,

            @ApiParam(value = "搜索关键词", required = false) @RequestParam(value = "keyword", required = false) String keyword,

            @ApiParam(value = "页码", required = false, defaultValue = "1") @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,

            @ApiParam(value = "每页条数", required = false, defaultValue = "10") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        // 限制最大页面大小
        if (pageSize > CommonConstants.MAX_PAGE_SIZE) {
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }

        return ApiResult.success(adminUserService.getUserList(role, status, keyword, page, pageSize));
    }

    /**
     * 获取用户详情
     */
    @ApiOperation("获取用户详情")
    @GetMapping("/{userId}")
    public ApiResult<UserDetailVO> getUserDetail(
            @ApiParam(value = "用户ID", required = true) @PathVariable("userId") Long userId) {
        return ApiResult.success(adminUserService.getUserDetail(userId));
    }

    /**
     * 更新用户状态
     */
    @ApiOperation("更新用户状态")
    @PutMapping("/{userId}/status")
    public ApiResult<Boolean> updateUserStatus(
            @ApiParam(value = "用户ID", required = true) @PathVariable("userId") Long userId,

            @Validated @RequestBody UserStatusParam param) {
        return ApiResult.success("用户状态已更新", adminUserService.updateUserStatus(userId, param));
    }
}
